<template>
  <view class="realname-container">
    <view class="form-section">
      <view class="form-header">
        <text class="subtitle">请填写您的真实信息</text>
      </view>
      <view class="form-item">
        <text class="label">真实姓名</text>
        <input type="text" :disabled="isCardAuth" placeholder="请输入真实姓名" v-model="name" class="input-field" />
      </view>
      <view class="form-item">
        <text class="label">身份证号</text>
        <input type="text" :disabled="isCardAuth" placeholder="请输入身份证号码" v-model="card" class="input-field" />
      </view>
      <button :disabled="isCardAuth"  :class="isCardAuth?'submit-ntm disabled ':'submit-btn'" @click="handleSubmit">
        {{ isCardAuth ? '已认证' : '同意授权并认证'}}
      </button>
    </view>
  </view>
</template>

<script>
import { realName } from '@/api/login'
export default {
  data() {
    return {
      name: this.$store.state.user.name||'',
      userId: this.$store.state.user.id,
      card: this.$store.state.user.card||'',
      isCardAuth: false, // 身份证认证状态
    }
  },
  created() {
    if(this.$store.state.user.name){
      this.isCardAuth = true
    }
  },
  methods: {
    validateForm() {
      if (!this.name) {
        uni.showToast({
          title: '请输入真实姓名',
          icon: 'none'
        })
        return false
      }

      // 身份证号校验正则
      const idCardReg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/
      if (!this.card) {
        uni.showToast({
          title: '请输入身份证号码',
          icon: 'none'
        })
        return false
      } else if (!idCardReg.test(this.card)) {
        uni.showToast({
          title: '身份证号码格式不正确',
          icon: 'none'
        })
        return false
      }

      return true
    },
    getUserInfo() {
      this.$store.dispatch('GetInfo').then(() => {
       this.isCardAuth = true
      })
    },
    handleSubmit() {
      if (!this.validateForm()) return

      realName({
        name: this.name,
        card: this.card,
        userId: this.userId
      }).then(res => {
        if (res.code === 200) {
          uni.showToast({
            title: '认证提交成功',
            icon: 'success'
          })
          this.getUserInfo()
          // 这里添加提交逻辑

        }
      })

    }
  }
}
</script>


<style lang="scss" scoped>
.realname-container {
  padding: 40rpx;
  background: linear-gradient(to bottom, #f5f7fa, #e8eff5);
  min-height: 100vh;

  .form-section {
    background: #fff;
    border-radius: 24rpx;
    padding: 40rpx;
    box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);

    .form-header {
      margin-bottom: 60rpx;
      text-align: center;

      .title {
        font-size: 36rpx;
        font-weight: bold;
        color: #333;
        display: block;
        margin-bottom: 10rpx;
      }

      .subtitle {
        font-size: 26rpx;
        color: #999;
      }
    }

    .form-item {
      margin-bottom: 50rpx;

      .label {
        display: block;
        margin-bottom: 20rpx;
        font-size: 30rpx;
        color: #333;
        font-weight: 500;
      }

      .input-field {
        width: 100%;
        height: 90rpx;
        border: 2rpx solid #e1e5eb;
        border-radius: 12rpx;
        padding: 0 30rpx;
        box-sizing: border-box;
        font-size: 28rpx;
        transition: border-color 0.3s;

        &:focus {
          border-color: #1989fa;
        }

        &::placeholder {
          color: #c0c4cc;
        }
      }
    }

    .submit-btn {
      width: 100%;
      height: 96rpx;
      line-height: 96rpx;
      background: linear-gradient(to right, rgba(0,0,0,0.8), #000);
      color: #fff;
      border-radius: 48rpx;
      margin-top: 80rpx;
      font-size: 32rpx;
      font-weight: 500;
      border: none;
      box-shadow: 0 8rpx 20rpx rgba(30, 99, 250, 0.2);
      transition: all 0.3s;
      &.disabled {
        background: #e1e5eb;
        color: #c0c4cc;
        cursor: not-allowed;
      }
      &:active {
        transform: translateY(2rpx);
        box-shadow: 0 4rpx 10rpx rgba(30, 99, 250, 0.2);
      }
    }
  }
}
</style>
